{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.5 64-bit"
  },
  "interpreter": {
   "hash": "6de75d21c2594102d9276817922b7fd0f8914985ad781a480f159997b19750b6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pdfplumber\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#该文件已被运行，但也可重复运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pdf=pdfplumber.open(\".\\\\sources\\\\ufo.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls=[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for page in pdf.pages:\n",
    "    for row in page.extract_tables()[0]:\n",
    "        dt={}\n",
    "        dt['City']=row[0]\n",
    "        dt['Colors Reported']=row[1]\n",
    "        dt['Shape Reported']=row[2]\n",
    "        dt['State']=row[3]\n",
    "        dt['Time']=row[4]\n",
    "        ls.append(dt)\n",
    "pdf.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.DataFrame(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=df.iloc[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\".\\\\sources\\\\ufo_test.csv\",index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                       City Colors Reported Shape Reported State  \\\n",
       "1                    Ithaca                       TRIANGLE    NY   \n",
       "2               Willingboro            None          OTHER    NJ   \n",
       "3                   Holyoke                           OVAL    CO   \n",
       "4                   Abilene                           DISK    KS   \n",
       "5      New York Worlds Fair            None          LIGHT    NY   \n",
       "...                     ...             ...            ...   ...   \n",
       "18237            Grant Park            None       TRIANGLE    IL   \n",
       "18238           Spirit Lake                           DISK    IA   \n",
       "18239           Eagle River            None                   WI   \n",
       "18240            Eagle Rive            rRED          LIGHT    WI   \n",
       "18241                  Ybor                           OVAL    FL   \n",
       "\n",
       "                   Time  \n",
       "1        6/1/1930 22:00  \n",
       "2       6/30/1930 20:00  \n",
       "3       2/15/1931 14:00  \n",
       "4        6/1/1931 13:00  \n",
       "5       4/18/1933 19:00  \n",
       "...                 ...  \n",
       "18237  12/31/2000 23:00  \n",
       "18238  12/31/2000 23:00  \n",
       "18239  12/31/2000 23:45  \n",
       "18240  12/31/2000 23:45  \n",
       "18241  12/31/2000 23:59  \n",
       "\n",
       "[18241 rows x 5 columns]"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City</th>\n      <th>Colors Reported</th>\n      <th>Shape Reported</th>\n      <th>State</th>\n      <th>Time</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>Ithaca</td>\n      <td></td>\n      <td>TRIANGLE</td>\n      <td>NY</td>\n      <td>6/1/1930 22:00</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Willingboro</td>\n      <td>None</td>\n      <td>OTHER</td>\n      <td>NJ</td>\n      <td>6/30/1930 20:00</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Holyoke</td>\n      <td></td>\n      <td>OVAL</td>\n      <td>CO</td>\n      <td>2/15/1931 14:00</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Abilene</td>\n      <td></td>\n      <td>DISK</td>\n      <td>KS</td>\n      <td>6/1/1931 13:00</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>New York Worlds Fair</td>\n      <td>None</td>\n      <td>LIGHT</td>\n      <td>NY</td>\n      <td>4/18/1933 19:00</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>18237</th>\n      <td>Grant Park</td>\n      <td>None</td>\n      <td>TRIANGLE</td>\n      <td>IL</td>\n      <td>12/31/2000 23:00</td>\n    </tr>\n    <tr>\n      <th>18238</th>\n      <td>Spirit Lake</td>\n      <td></td>\n      <td>DISK</td>\n      <td>IA</td>\n      <td>12/31/2000 23:00</td>\n    </tr>\n    <tr>\n      <th>18239</th>\n      <td>Eagle River</td>\n      <td>None</td>\n      <td></td>\n      <td>WI</td>\n      <td>12/31/2000 23:45</td>\n    </tr>\n    <tr>\n      <th>18240</th>\n      <td>Eagle Rive</td>\n      <td>rRED</td>\n      <td>LIGHT</td>\n      <td>WI</td>\n      <td>12/31/2000 23:45</td>\n    </tr>\n    <tr>\n      <th>18241</th>\n      <td>Ybor</td>\n      <td></td>\n      <td>OVAL</td>\n      <td>FL</td>\n      <td>12/31/2000 23:59</td>\n    </tr>\n  </tbody>\n</table>\n<p>18241 rows × 5 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}